package com.example.blog_system.controller;

import com.example.blog_system.dto.ApiResponse;
import com.example.blog_system.dto.CommentDto;
import com.example.blog_system.security.UserPrincipal;
import com.example.blog_system.service.CommentService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteComment(
            @PathVariable Long id,
            @AuthenticationPrincipal UserPrincipal currentUser) {
        commentService.deleteComment(id, currentUser.getId());
        return ResponseEntity.ok(ApiResponse.success("评论删除成功"));
    }

    /**
     * 获取文章的评论列表
     */
    @GetMapping("/article/{articleId}")
    public ResponseEntity<ApiResponse<Page<CommentDto>>> getCommentsByArticle(
            @PathVariable Long articleId,
            @PageableDefault(page = 0, size = 10, sort = "createdAt", direction = Sort.Direction.DESC) 
            Pageable pageable) {
        Page<CommentDto> comments = commentService.getCommentsByArticle(articleId, pageable);
        return ResponseEntity.ok(ApiResponse.success("获取评论列表成功", comments));
    }

    /**
     * 获取用户的评论列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<ApiResponse<Page<CommentDto>>> getCommentsByUser(
            @PathVariable Long userId,
            @PageableDefault(page = 0, size = 10, sort = "createdAt", direction = Sort.Direction.DESC) 
            Pageable pageable) {
        Page<CommentDto> comments = commentService.getCommentsByUser(userId, pageable);
        return ResponseEntity.ok(ApiResponse.success("获取用户评论列表成功", comments));
    }
} 